#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"
#include "wizchip_conf.h"
#include "bsp_spi.h"

/* Network information to be configured. */
wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x1e, 0xed, 0x2e}, // Configured MAC address
    .ip = {192, 168, 1, 10},                     // Configured IP address
    .sn = {255, 255, 255, 0},                    // Configured subnet mask
    .gw = {192, 168, 1, 1},                      // Configured gateway
    .dns = {8, 8, 8, 8},                         // Configured domain address
};

/* This parameter is used to receive the configuration information read back. */
wiz_NetInfo get_info;

int main()
{
    uint8_t link_status;
    wiz_PhyConf phyconf;
    uint32_t count = 0;

    stdio_init_all();     // Initialize the main control peripheral
    wizchip_initialize(); // Chip initialization

    printf("wiznet chip network install example.\r\n");
    network_initialize(net_info);         // Write configuration information
    print_network_information(&get_info); // Read the configuration information and print it

    do
    {
        link_status = wizphy_getphylink();
        if (link_status == PHY_LINK_OFF)
        {
            count++;
            if (count > 10)
            {
                printf("Link failed of Internal PHY.\r\n");
                break;
            }
        }
        sleep_ms(500);

    } while (link_status == PHY_LINK_OFF);

    if (link_status == PHY_LINK_ON)
    {
        wizphy_getphyconf(&phyconf);
        printf("Link OK of Internal PHY.\r\n");
        printf("the %d Mbtis speed of Internal PHY.\r\n", phyconf.speed == PHY_SPEED_100 ? 100 : 10);
        printf("The %s Duplex Mode of the Internal PHY.\r\n", phyconf.duplex == PHY_DUPLEX_HALF ? "Half-Duplex" : "Full-Duplex");

        printf("\r\nTry ping the ip:%d.%d.%d.%d.\r\n", get_info.ip[0], get_info.ip[1], get_info.ip[2], get_info.ip[3]);
    }
    else
    {
        printf("\r\nPlease check whether the network cable is loose or disconnected.\r\n");
    }

    while (true)
    {
    }
}
